package cn.unknowpupil.init.utils.thread;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Created at 2020/11/10 by w10g <br>
 * Initialize RunThread ... <br>
 *
 * @author unknowpupil
 * @since 0.0.1
 */
public class RunThread {
    private ThreadPoolTaskExecutor taskExecutor;

    public RunThread(Integer corePoolSize, Integer maxPoolSize) {
        taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.initialize();
        taskExecutor.setCorePoolSize(corePoolSize);
        taskExecutor.setMaxPoolSize(maxPoolSize);
    }

    /**
     * @param list   需要处理的list
     * @param sunSum 将集合切分的段数
     * @throws InterruptedException
     * @throws ExecutionException
     */
    @SuppressWarnings("unused")
    private void doReadList(List<String> list, int sunSum) throws InterruptedException, ExecutionException {
        /**初始化集合**/


        /**接收集合各段的 执行的返回结果**/
        List<Future<Boolean>> futureList = new ArrayList<Future<Boolean>>();

        /**集合总条数**/
        int size = list.size();
        /**将集合切分的段数**/
        //int sunSum = 10;
        int listStart, listEnd;
        /***当总条数不足10条时 用总条数 当做线程切分值**/
        if (sunSum > size) {
            sunSum = size;
        }
        /**定义子线程**/
        SunCallable sunCallable = null;
        /**将list 切分10份 多线程执行**/
        for (int i = 0; i < sunSum; i++) {
            /***计算切割  开始和结束**/
            listStart = size / sunSum * i;
            listEnd = size / sunSum * (i + 1);
            /**最后一段线程会 出现与其他线程不等的情况**/
            if (i == sunSum - 1) {
                listEnd = size;
            }
            /**线程切断**/
            List<String> sunList = list.subList(listStart, listEnd);
            /**子线程初始化**/
            sunCallable = new SunCallable(i, sunList);
            /***多线程执行***/
            futureList.add(taskExecutor.submit(sunCallable));
        }
        /**对各个线程段结果进行解析**/
        for (Future<Boolean> future : futureList) {
            if (null != future && future.get()) {
                System.err.println("成功");
            } else {
                System.err.println("失败");
            }
        }
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        for(int i=0;i<123;i++){
            list.add("test--"+i);
        }
        long start=System.currentTimeMillis();
        RunThread   runThread=new RunThread(32,32);
        try {
            runThread.doReadList(list,8);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("执行程序用时："+(System.currentTimeMillis()-start));
    }

}


